package com.bsoft.exchange.service;

import com.bsoft.exchange.dao.his.PublicMapper;
import com.bsoft.exchange.domain.*;
import com.bsoft.exchange.pojo.MedicalCardPojo;
import com.bsoft.exchange.pojo.PatientIdentityCardPojo;
import com.bsoft.exchange.util.BaseResponse;
import com.bsoft.exchange.util.CacheSupport;
import com.bsoft.exchange.util.Constant;
import com.bsoft.exchange.util.TextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

/**
 * 创建日期 : 2019/6/19
 * 创建人 : 赵太行
 * 公共的服务
 */
@Service
public class PublicService {
    /**
     * 获取病人集合
     * @param queryType  查询类型
     * @param patientIdentityCardPojoList
     * @param medicalCardPojoList
     * @return
     */
    @Autowired
    PublicMapper publicMapper;
    @Autowired
    CacheSupport cacheSupport;
    private List<String> jzkhList;
    private List<String> ybkhList;
    private List<String> reidList;
    private List<String> zyhList;
    private String brid;
    public List<String>getBRIDist(String jgid,Integer queryType, List<PatientIdentityCardPojo>patientIdentityCardPojoList,
                                  List<MedicalCardPojo> medicalCardPojoList){
        if (reidList==null){
            reidList=new ArrayList<>();
        }else{
            reidList.clear();
        }
        if (queryType.equals(2)){
            //如果是证件查询
            if (patientIdentityCardPojoList.size()==0){
                return null;
            }
            reidList = publicMapper.getBridList(patientIdentityCardPojoList);
        }else if (queryType.equals(3)){
            if (jzkhList==null) {
                jzkhList = new ArrayList<>();
            }else{
                jzkhList.clear();
            }
            if (ybkhList==null) {
                ybkhList = new ArrayList<>();
            }else{
                ybkhList.clear();
            }
            if (zyhList==null){
                zyhList=new ArrayList<>();
            }else{
                zyhList.clear();
            }
            //诊疗卡查询
            for (int i = 0; i < medicalCardPojoList.size(); i++) {
                if (!"".equals(medicalCardPojoList.get(i).getPatientMedicalCardNumber())){
                    if (medicalCardPojoList.get(i).getPatientMedicalCardNumberType() == 2) {
                        jzkhList.add(medicalCardPojoList.get(i).getPatientMedicalCardNumber());
                    } else if (medicalCardPojoList.get(i).getPatientMedicalCardNumberType() == 3) {
                        ybkhList.add(medicalCardPojoList.get(i).getPatientMedicalCardNumber());
                    } else if (medicalCardPojoList.get(i).getPatientMedicalCardNumberType() == 5){
                        zyhList.add(medicalCardPojoList.get(i).getPatientMedicalCardNumber());
                    }else{
                        reidList.clear();
                        return reidList;
                    }
                }

            }
            if (ybkhList==null && jzkhList==null&& zyhList==null){
                return null;
            }
            if (jzkhList.size()>0) {
                reidList.addAll(publicMapper.getJZKHBridList(jzkhList));
            }
            if (ybkhList.size()>0){
                reidList.addAll(publicMapper.getYBKHBridList(ybkhList));
            }
            if (zyhList.size()>0){
                reidList.addAll(publicMapper.getZYHBridList(jgid,zyhList));
            }
        }
        return reidList;
    }
    public String getBRID(String jgid,Integer queryType,String cardType,String cardNumber,
                                  Integer patientMedicalCardNumberType,String patientMedicalCardNumber){
        if (queryType.equals(2)){
            //如果是证件查询
            if (cardType==null|| TextUtils.isEmpty(cardNumber)){
                return null;
            }
            brid = publicMapper.getBrid(queryType,cardType,cardNumber,null,null);
        }else if (queryType.equals(3)){
            if (TextUtils.isEmpty(patientMedicalCardNumber)||patientMedicalCardNumberType==null){
                return null;
            }
            if (patientMedicalCardNumberType==2) {
                brid = publicMapper.getBrid(queryType, null, null, patientMedicalCardNumber, null);
                }else if (patientMedicalCardNumberType==3){
                brid = publicMapper.getBrid(queryType, null, null, null, patientMedicalCardNumber);
                }else if (patientMedicalCardNumberType==5){
                brid=publicMapper.getZyBrid(jgid,patientMedicalCardNumber);
            }
            }else{
                return null;
            }
        return brid;
    }
    public BaseResponse<String> getPjdm(String jgid, String ygdm, Integer pjlx)throws Exception{
        BaseResponse<String> baseResponse=new BaseResponse();
        ReceiptDomain receiptDomain= publicMapper.getReceipt(jgid, ygdm,pjlx );
        if (receiptDomain==null){
            return null;
        }
        String zxpjdm;
        Integer sybz=0;
        //回插入表
        Pattern p = Pattern.compile("(?<=\\D)(?=\\d+\\b)");
        String DQHM[] = receiptDomain.getDQHM().split(p.toString());
        String QSHM[] = receiptDomain.getQSHM().split(p.toString());
        String ZZHM[] = receiptDomain.getZZHM().split(p.toString());
        if (DQHM.length==1){
            //如果等于空则说明是纯数字组成
            if (Integer.parseInt(DQHM[0])>Integer.parseInt(ZZHM[0])){
                //如果当前号码大于最终号码则提示
                baseResponse.setCode(Constant.errorTag);
                baseResponse.setMessage("票据已经用完");
                return baseResponse;
            }
            Integer qq=Integer.parseInt(DQHM[0])+1;
            zxpjdm=String.format("%0"+DQHM[0].length()+"d",qq);
            if ((Integer.parseInt(DQHM[0])+1)==Integer.parseInt(ZZHM[0])){
                sybz=1;
            }
        }else{
            if (Integer.parseInt(DQHM[1])>Integer.parseInt(ZZHM[1])){
                //如果当前号码大于最终号码则提示
                baseResponse.setCode(Constant.errorTag);
                baseResponse.setMessage("票据已经用完");
                return baseResponse;
            }
            //将前后合并起来
            Integer qq=Integer.parseInt(DQHM[1])+1;
            zxpjdm=DQHM[0]+ String.format("%0"+DQHM[1].length()+"d",qq);
            if ((Integer.parseInt(DQHM[1])+1)==Integer.parseInt(ZZHM[1])){
                sybz=1;
            }
        }
        baseResponse.setCode(Constant.successResponse);
        baseResponse.setData(receiptDomain.getDQHM());
        //将次票据+1后反写进数据库
        publicMapper.updateReceipt(jgid,ygdm,pjlx,zxpjdm,sybz);
        return baseResponse;
    }
    public PatientInfoDomain getPatient(String brid,String ysdm,String ksdm,String jgid){
        return publicMapper.getPatient(brid, ysdm, ksdm, jgid);
    }

    /**
     * 获取药品名称
     * @return
     */
    public List<DrugInfo>getDrugInfo(){
        return cacheSupport.getDrugInfo(publicMapper);
    }
    /**
     * 获取药品产地
     * @return
     */
    public List<DrugField>getDrugField(){
        return cacheSupport.getDrugField(publicMapper);
    }
    /**
     * 获取医生信息
     * @param jgid
     * @return
     */
    public List<DoctorInfo>getDoctorInfo(String jgid){
        return cacheSupport.getDoctorInfo(jgid,jgid+"jgmc",publicMapper);
    }
    /**
     * 获取收费项目
     * @return
     */
    public List<ChargeProject>getSfxm(){
        return cacheSupport.getSfxmInfo(publicMapper);
    }
    /**
     * 获取科室代码
     * @param jgid
     * @return
     */
    public List<KsdmDomain>getKsdm(String jgid){
        return cacheSupport.getKsdmInfo(jgid,jgid+"jgmc",publicMapper);
    }
    /**
     * 病人性质
     * @return
     */
    public List<BrxzInfo>getBrxzList(){
        return cacheSupport.getBrxzInfo(publicMapper);
    }
    /**
     * 获取处方效期
     */
}
